package com.agu.web;

import java.sql.SQLException;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.apache.shiro.authz.UnauthorizedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import com.agu.result.Result;
import com.agu.result.ResultGenerator;
import com.agu.utils.RRException;


/**
 * 异常处理器
 * 
 */

@RestControllerAdvice
public class GlobalDefaultExceptionHandler {

	private Logger logger = LoggerFactory.getLogger(getClass());

	/**
	 * 未登录
	 */
	@ExceptionHandler({ UnauthenticatedException.class, AuthenticationException.class })
	@ResponseBody
	public Result authenticationException(HttpServletRequest request) {
		logger.error("========未登录,请先登录=======");
		return ResultGenerator.loginError("未登录,请先登录");
	}
	
	/**
	 * 没有权限
	 * @param e
	 * @return
	 */
	@ExceptionHandler({ UnauthorizedException.class, AuthorizationException.class })
	@ResponseBody
	public Result handleAuthorizationException(HttpServletRequest request) {
		logger.error("========没有权限=======");
		return   ResultGenerator.noPermissions();
	}
	/**
	 * 数据库中已存在该记录
	 * @param e
	 * @return
	 */
	@ExceptionHandler(DuplicateKeyException.class)
	@ResponseBody
	public Result handleDuplicateKeyException(DuplicateKeyException e) {
		logger.error("========数据库中已存在该记录======");
		logger.error(e.getMessage(), e);
		return ResultGenerator.fail("数据库中已存在该记录");
	}
	
	@ExceptionHandler(SQLException.class)
	@ResponseBody
	public Result handleSQLExceptionException(SQLException e) {
		logger.error("========数据库错误======");
		logger.error(e.getMessage(), e);
		return ResultGenerator.fail("数据库错误:"+e.getMessage());
	}


	/**
	 * 不支持请求的方法
	 * @param e
	 * @return
	 */
	@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
	@ResponseBody
	public Result handleHttpRequestMethodNotSupportedException(HttpRequestMethodNotSupportedException e) {
		logger.error("=======不支持请求的方法=======");
		logger.error(e.getMessage(), e);
		return ResultGenerator.fail(e.getMessage());
	}
	
	@ExceptionHandler({RRException.class})
	@ResponseBody
	public Result rrException(RRException e) {
		logger.error("========业务异常 RRException=======");
		return ResultGenerator.loginError(e.getMessage());
	}
	
	/**
	 * 运行错误
	 * @param e
	 * @return
	 */
	@ExceptionHandler(value = RuntimeException.class)
	@ResponseBody
	public Result defaultExceptionHandler(RuntimeException e) {
		logger.error("=======服务器异常=======");
		logger.error(e.getMessage(), e);
		return ResultGenerator.serverFail("服务器异常");
	}


	
	/**
	 * 未知错误
	 * @param e
	 * @return
	 */
	@ExceptionHandler(Exception.class)
	@ResponseBody
	public Result handleException(Exception e) {
		logger.error("=======未知错误=======");
		logger.error(e.getMessage(), e);
		return ResultGenerator.fail("未知错误");
	}

}